//
//  TableViewCell.m
//  YouinRTCDemo
//
//  Created by 潘欣 on 2024/6/16.
//

#import "TableViewCell.h"
@interface TableViewCell ()
@property (nonatomic,strong)UIView *bgView;
@property (nonatomic,strong)UILabel *statue;
@property (nonatomic,strong)UIImageView *bgImg;
@property (nonatomic,strong)UILabel *time;
@property (nonatomic,strong)UILabel *centerLabel;
@property (nonatomic,strong)UILabel *name;

@end
@implementation TableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.bgView = [[UIView alloc] init];
        self.bgView.layer.cornerRadius = 10;
        self.bgView.clipsToBounds = YES;
        self.bgView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.bgView];
        
        
        self.bgImg = [[UIImageView alloc] init];
        self.bgImg.contentMode = UIViewContentModeScaleAspectFill;
        self.bgImg.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        self.bgImg.clipsToBounds = YES;
        self.bgImg.backgroundColor = rgba(255, 99, 98, 0.8);
        [self.bgView addSubview:self.bgImg];
        
        
        self.statue = [[UILabel alloc] init];
        self.statue.text = @"主播不在";
        self.statue.backgroundColor = [UIColor colorWithWhite:0.3 alpha:1.0];
        self.statue.textAlignment = 1;
        self.statue.textColor = [UIColor whiteColor];
        self.statue.font = [UIFont systemFontOfSize:11 weight:UIFontWeightRegular];
        [self.bgView addSubview:self.statue];
        
        self.name = [[UILabel alloc] init];
        self.name.text = @"主播不11111111在";
        self.name.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
        [self.bgView addSubview:self.name];
        
        
        self.time = [[UILabel alloc] init];
        self.time.text = @"直播时间：2022-08-07 15:05";
        self.time.textColor = [UIColor grayColor];
        self.time.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
        [self.bgView addSubview:self.time];
        
        
        self.centerLabel = [[UILabel alloc] init];
        self.centerLabel.text = @"主播不在";
        self.centerLabel.textColor = [UIColor whiteColor];
        self.centerLabel.textAlignment = 1;
        self.centerLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightRegular];
        [self.bgImg addSubview:self.centerLabel];
        
        
        _shopCart = [UIButton buttonWithType:UIButtonTypeCustom];
        [_shopCart setBackgroundImage:[UIImage imageNamed:@"bag"] forState:UIControlStateNormal];
        _shopCart.titleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
        [_shopCart setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [self.bgView addSubview:_shopCart];
        
        
        _shareLink = [UIButton buttonWithType:UIButtonTypeCustom];
        [_shareLink addTarget:self action:@selector(shareLikeAction) forControlEvents:UIControlEventTouchUpInside];
        [_shareLink setImage:[UIImage imageNamed:@"qn_more"] forState:UIControlStateNormal];
        [self.bgView addSubview:_shareLink];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    float viewWidth = (SCREEN_WIDTH-30);
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(15);
        make.right.mas_equalTo(self).offset(-15);
        make.top.mas_equalTo(self).offset(10);
        make.height.offset(viewWidth*0.8);
    }];

    
    float imgHeight = viewWidth*0.6;
    [self.bgImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.right.mas_equalTo(self.bgView);
        make.height.offset(imgHeight);
    }];
    
    [self.statue mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self.bgView);
        make.width.offset(80);
        make.height.offset(25);
    }];
    
    [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bgImg).offset(10);
        make.top.mas_equalTo(self.bgImg.mas_bottom).offset(10);
        make.height.offset(25);
    }];
    
    [self.time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.name);
        make.top.mas_equalTo(self.name.mas_bottom).offset(7);
    }];
    
    [self.centerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self.bgImg);
        
    }];
    
   
    [self.shareLink mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.bgView).offset(-10);
        make.width.height.offset(35);
        make.top.mas_equalTo(self.bgImg.mas_bottom).offset(15);
    }];
    
    [self.shopCart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.shareLink.mas_left).offset(-10);
        make.top.mas_equalTo(self.shareLink);
        make.width.height.offset(35);
    }];
}

- (void)shareLikeAction
{
    NSLog(@"shareLikeAction");
}

- (void)setModel:(ResultModel *)model{
    _model = model;
    if (model.live_status.status == 4) {//未开始
        self.statue.backgroundColor = [UIColor redColor];
        self.centerLabel.text = @"直播尚未开始";
    }else if (model.live_status.status == 3)//已结束
    {
        self.centerLabel.text = @"直播已结束";
        self.statue.backgroundColor = [UIColor colorWithWhite:0.3 alpha:1.0];
    }else if (model.live_status.status == 2)//2直播不在
    {
        self.centerLabel.text = @"主播不在";
        self.statue.backgroundColor = [UIColor orangeColor];
    }else{//直播中
        self.centerLabel.text = @"直播中";
        self.statue.backgroundColor = [UIColor orangeColor];
    }
    self.statue.text = model.live_status.info;
    
    [self setView];
    [self.bgImg sd_setImageWithURL:[NSURL URLWithString:model.image]];
    self.name.text = model.title;
    
    NSString *formattedDate = [self formatDateString:model.start_time];
    self.time.text = [NSString stringWithFormat:@"直播时间：%@",formattedDate];
    [self.shopCart setTitle:model.product_count forState:UIControlStateNormal];
}

- (NSString *)formatDateString:(NSString *)dateString {
    // 1. 检查输入是否为空
    if (dateString.length == 0) {
        return @"无日期";
    }
    
    // 2. 配置ISO 8601解析器（固定区域+格式）
    NSDateFormatter *isoFormatter = [[NSDateFormatter alloc] init];
    isoFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]; // 避免本地化影响
    isoFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss";
    
    // 3. 解析日期
    NSDate *date = [isoFormatter dateFromString:dateString];
    if (!date) {
        return @"日期格式错误";
    }
    
    // 4. 配置输出格式（根据系统设置选择12/24小时制）
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    if ([Tool is24HourFormat]) {
        outputFormatter.dateFormat = @"yyyy年M月d日 HH:mm"; // 24小时制
    } else {
        outputFormatter.dateFormat = @"yyyy年M月d日 ah:mm"; // 12小时制
        outputFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"]; // 中文上午/下午
    }
    
    return [outputFormatter stringFromDate:date];
}

-(void)setView{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 80, 25) byRoundingCorners: UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.statue.bounds;
    maskLayer.path = maskPath.CGPath;
    self.statue.layer.mask = maskLayer;
}
@end
